<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-form :inline='true'
               :model="roleQueryParam"
               class="demo-form-inline">
        <el-form-item label="角色名">
          <el-input v-model="roleQroleName"
                   placeholder="请输入角色名"
                   clearable
                   @clear="getRoleList"></el-input>
        </el-form-item> 
        <el-form-item>
          <el-button type="primary"
                    @click="getRoleList">查询</el-button>
        </el-form-item>  
        <el-button type="primary" 
                  @click="addDialogVisible = true" 
                  style="float: right" >添加角色</el-button> 
      </el-form>
      <!-- 角色列表区域 -->
      <el-table :data="this.pageBean.list"
              ref="multipleTable"
              tooltip-effect="dark"
              style="width: 100%">
        <el-table-column label="角色名"
                         prop="roleName"></el-table-column>
        <el-table-column label="描述"
                         prop="roleDesc"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.state"
                       @change="roleStateChanged(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间"
                         prop="createTime"></el-table-column>
        <el-table-column label="操作"
                         width="200px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary"
                       icon="el-icon-edit"
                       size="mini"
                       @click="showEditDialog(scope.row.id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger"
                       icon="el-icon-delete"
                       size="mini"
                       @click="removeRoleById(scope.row.id)"></el-button>
            <!-- 分配角色权限 -->
            <el-tooltip effect="dark"
                        content="分配角色权限"
                        placement="top"
                        :enterable="false">
              <el-button type="warning"
                         icon="el-icon-setting"
                         size="mini"
                         @click="setRole(scope.row.id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
       <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="pageBean.pageNum"
                     :page-sizes="[1, 2, 5, 10]"
                     :page-size="pageBean.pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="pageBean.total">
      </el-pagination>
    </el-card>
    <!-- 添加角色的对话框 --> 
    <el-dialog title="添加角色"
               :visible.sync="addDialogVisible"
               width="50%"
               @close="addDialogClosed">
      <!-- 对话框内容区域 -->
      <el-form :model="addForm"
               ref="addFormRef"
               label-width="70px">
        <el-form-item label="角色名"
                      prop="roleName">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述"
                      prop="roleDesc">
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
        <el-form-item label="状态"
                      prop="state">
          <el-input v-model="addForm.state"></el-input>
        </el-form-item>
        <!-- <el-form-item label="创建时间" required>          
          <el-date-picker v-model="addForm.createTime"
                          type="date"
                          placeholder="选择日期"
                          :picker-options="end"
                          >
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addRole">确 定</el-button>
      </span>
    </el-dialog>
     <!-- 修改角色的对话框 -->
    <el-dialog title="修改角色"
               :visible.sync="editDialogVisible"
               width="50%"
               @close="editDialogClosed">
      <el-form :model="editForm"
               ref="editFormRef"
               label-width="70px">
        <el-form-item label="角色名">
          <el-input v-model="editForm.roleName"
                    disabled></el-input>
        </el-form-item>
        <el-form-item label="角色描述"
                      prop="roleDesc">
          <el-input v-model="editForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="editRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default{
  //数据
   data(){
    return{
      roleQueryParam: {  //添加查询参数，对应后端的实体类
        roleName: '',
        pageNum: 2,
        pageSize: 8
      },
      pageBean: {  //分页数据
        total: 0,
        pages: 0,
        pageNum: 0,
        pageSize: 0,
        list: [],  //表示后台的User集合
      },
      // 控制添加角色对话框的显示与隐藏
      addDialogVisible: false,

      // 添加角色的表单数据
      addForm: {
        roleName: '',
        roleDesc: '',
        state: '',
       // createTime: ''
      },
      editForm:{}
    }
  },
  //生命周期
  created(){
    this.getRoleList()
  },

  //方法
  methods:{
    //得到角色列表,并且根据查询条件展示
    getRoleList () {
      console.log(this.roleQueryParam)
      this.axios.post(this.baseUrl + "role/getAllRole", this.roleQueryParam)
        .then(res => {
          console.log(this.roleQueryParam)
          if (res.data.status === 'SUCCESS') {
            console.log(res.data.data)
            //将数据传给pageBean
            this.pageBean = res.data.data
            this.$message({
              type: 'success',
              message: res.data.message
            });
          } else {
            this.$message({
              type: 'warning',
              message: res.data.message,
            });
            console.log(res.data.message)
          }
        })
    },
    // 监听 pageSize 改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      console.log(newSize)
      this.roleQueryParam.pageSize = newSize
      console.log(this.roleQueryParam.pageSize)
      this.getRoleList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      console.log(newPage)
      this.roleQueryParam.pageNum = newPage
      this.getRoleList()
    },
    // 监听添加角色对话框的关闭事件,按取消或确定，
    //如果按取消，addFormRef.validate为fales,确定则为true
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 当确定按钮按下，添加新角色
    addRole () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加角色的网络请求
        const { data: res } = await this.axios.post(this.baseUrl+'role/insert', this.addForm)

        if (res.data.code !== 201) {
          this.$message.error('添加角色失败！')
        }

        this.$message.success('添加角色成功！')
        // 隐藏添加角色的对话框
        this.addDialogVisible = false
        // 重新获取角色列表数据
        this.getRoleList()
      })
    },
    //展示编辑框
    showEditDialog (id){
        console.log(id)
        this.axios.get(this.baseUrl+"role/getById/"+id)
            .then(res=>{
               if (res.data.status === 'SUCCESS') {
                console.log(res.data.data)
               this.editForm = res.data.data
               this.editDialogVisible = true
            this.$message({
              type: 'success',
              message: res.data.message
            });
          } else {
            this.$message({
              type: 'warning',
              message: res.data.message,
            });
            console.log(res.data.message)
          }
            })
    },
    // 监听修改角色对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改编辑框中角色信息并提交
    editRole () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return   //如果无效，直接return
        this.axios.put(this.baseUrl+ 'role/update/',this.editForm)
                .then(res=>{
                   if (res.data.status ==='SUCCESS') {
                      // 关闭对话框
                      this.editDialogVisible = false
                      // 刷新数据列表
                      this.getRoleList()
                      // 提示修改成功
                      console.log(res.data.data)
                      return this.$message.success('更新角色信息成功！')
                    }
                      console.log(res.data.message)
                      this.$message.error('更新角色信息失败！')
                            })   
      })
    },
    

      



  }

}
 





















</script>